home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume11 / cpmod < prev    next >
Encoding:
Internet Message Format  |  1987-08-31  |  8.0 KB

  1. Path: uunet!rs
  2. From: rs@uunet.UU.NET (Rich Salz)
  3. Newsgroups: comp.sources.unix
  4. Subject: v11i027:  Copy modes/ownerships/times
  5. Message-ID: <1333@uunet.UU.NET>
  6. Date: 2 Sep 87 00:13:30 GMT
  7. Organization: UUNET Communications Services, Arlington, VA
  8. Lines: 305
  9. Approved: rs@uunet.UU.NET
  10.  
  11. Submitted-by: psivax!sa@ttidca.tti.com (Steve Alter)
  12. Posting-number: Volume 11, Issue 27
  13. Archive-name: cpmod
  14.  
  15. Cpmod(1) is a simple inode-information copier.  It will copy modes
  16. and/or ownerships and/or timestamps from an existing file to another
  17. existing file without modifying the data.  A detailed introduction
  18. is in the sharfile below in file "cpmod.intro".
  19. -------------CUT---------------CUT----------------CUT-----------------
  20. #! /bin/sh
  21. # This is a shell archive, meaning:
  22. # 1. Remove everything above the #! /bin/sh line.
  23. # 2. Save the resulting text in a file.
  24. # 3. Execute the file with /bin/sh (not csh) to create:
  25. #    cpmod.intro
  26. #    Makefile
  27. #    cpmod.1
  28. #    cpmod.c
  29. export PATH; PATH=/bin:/usr/bin:$PATH
  30. echo shar: "extracting 'cpmod.intro'" '(2087 characters)'
  31. if test -f 'cpmod.intro'
  32. then
  33.     echo shar: "will not over-write existing file 'cpmod.intro'"
  34. else
  35. sed 's/^X//' << \SHAR_EOF > 'cpmod.intro'
  36. XHere is a simple command called "cpmod" that fits in the same group as
  37. Xchmod, chown and touch.
  38. X
  39. XWhat is "cpmod"?  Well, imagine these:
  40. X
  41. XSituation #1:
  42. X     You've got two files with different data, different modes,
  43. Xdifferent ownerships and different times, but you realize that the
  44. Xmodes, ownerships and times (or maybe just the modes and times, or
  45. Xmaybe just the ownerships for example) really should be the same
  46. Xbetween the two files.  What do you do?  You could do "ls -lg file1"
  47. Xthen do appropriate chmod, chown, chgrp and /usr/5bin/touch commands
  48. Xon file2, but you'd like something easier.  (/usr/5bin/touch has the
  49. Xoption to specify an explicit timestamp to place on the file.)
  50. X
  51. XSituation #2:
  52. X     You've got a directory full of dozens or hundreds of programs
  53. Xthat should all have the same ownerships and modes, but you notice
  54. Xsome inconsistencies.  What do you do?  You could do "ls -lg program"
  55. Xon one of the correct ones, then do appropriate chmod, chown and chgrp
  56. Xcommands on all of the wrong ones, but that's a BIG hassle.
  57. X
  58. XSolution:
  59. X     Cpmod is a program which copies modes, ownerships and times from
  60. Xone existing file or directory to other existing files (or
  61. Xdirectories) without modifying the data in any of the destination
  62. Xfiles.  After this program is run, an "ls -lgd" command will show
  63. Xidentical information for all of the files specified on the command
  64. Xline, except for size and name.  All of this is, of course, subject to
  65. Xaccess permissions on the files and pathnames.  There are options to
  66. Xomit the modes and/or the ownerships and/or the times from the
  67. Xtransfer, but if all three are omitted then there's nothing left to do
  68. Xand the program complains.
  69. X
  70. XAnswer to Situation #1:
  71. X     cpmod file1 file2
  72. X
  73. XAnswer to Situation #2:
  74. X     cd directory ; cpmod -t correct_file *
  75. XNote that this will copy modes and ownerships from correct_file to all
  76. Xof the files in the whole directory, including correct_file, but that
  77. Xlittle redundancy is harmless.
  78. X
  79. X-- Steve Alter
  80. X...!{csun,trwrb,psivax}!ttidca!alter  or  alter@tti.com
  81. XCiticorp/TTI, Santa Monica CA  (213) 452-9191 x2541
  82. SHAR_EOF
  83. if test 2087 -ne "`wc -c < 'cpmod.intro'`"
  84. then
  85.     echo shar: "error transmitting 'cpmod.intro'" '(should have been 2087 characters)'
  86. fi
  87. fi
  88. echo shar: "extracting 'Makefile'" '(548 characters)'
  89. if test -f 'Makefile'
  90. then
  91.     echo shar: "will not over-write existing file 'Makefile'"
  92. else
  93. sed 's/^X//' << \SHAR_EOF > 'Makefile'
  94. XPROGRAM = cpmod
  95. X
  96. XOBJECTS = cpmod.o
  97. X
  98. XSOURCES = cpmod.c
  99. X
  100. XCFLAGS    = -O
  101. X
  102. XLFLAGS    = -abchx
  103. X
  104. XMANPAGE    = cpmod.1
  105. X
  106. XFILES    = cpmod.intro Makefile $(MANPAGE) $(SOURCES)
  107. X
  108. XPROGDIR    = /usr/local
  109. X
  110. XMANDIR    = /usr/man/man1
  111. X
  112. X$(PROGRAM): $(OBJECTS)
  113. X    cc $(CFLAGS) -o $(PROGRAM) $(OBJECTS)
  114. X
  115. Xlint: lint.err
  116. X
  117. Xlint.err: $(SOURCES)
  118. X    lint $(LFLAGS) $(SOURCES) > lint.err
  119. X
  120. Xinstall:
  121. X    install -s $(PROGRAM) $(PROGDIR)
  122. X    install -c $(MANPAGE) $(MANDIR)
  123. X
  124. Xdist shar: sharfile
  125. X
  126. Xsharfile: $(FILES)
  127. X    shar -a -p X $(FILES) > sharfile
  128. X
  129. Xclean:
  130. X    rm -f $(PROGRAM) $(OBJECTS) lint.err sharfile
  131. X
  132. SHAR_EOF
  133. if test 548 -ne "`wc -c < 'Makefile'`"
  134. then
  135.     echo shar: "error transmitting 'Makefile'" '(should have been 548 characters)'
  136. fi
  137. fi
  138. echo shar: "extracting 'cpmod.1'" '(1126 characters)'
  139. if test -f 'cpmod.1'
  140. then
  141.     echo shar: "will not over-write existing file 'cpmod.1'"
  142. else
  143. sed 's/^X//' << \SHAR_EOF > 'cpmod.1'
  144. X.TH CPMOD 1 "August 13, 1987"
  145. X.UC 4
  146. X.SH NAME
  147. Xcpmod \- copy modes, ownerships and times
  148. X.SH SYNOPSIS
  149. X.B cpmod
  150. X[
  151. X.B \-m
  152. X] [
  153. X.B \-o
  154. X] [
  155. X.B \-t
  156. X] file other-files ...
  157. X.SH ORIGIN
  158. X4.3BSD
  159. X.SH DESCRIPTION
  160. XThe modes, ownerships and times of
  161. X.I file
  162. Xare set into the inodes of all of the
  163. X.IR other-files .
  164. XIf the
  165. X.BR \-m ,
  166. X.B \-o
  167. Xand/or
  168. X.B \-t
  169. Xoptions are given, then the modes, ownerships and/or times
  170. Xrespectively do not participate in the transfer.
  171. X.SH SEE ALSO
  172. Xchmod(1), chown(1), utimes(2)
  173. X.SH DIAGNOSTICS
  174. XIf all three of the options are specified, then no work remains to be done
  175. Xand an appropriate error-message is issued.
  176. X.SH RESTRICTIONS
  177. XEach file actually has three timestamps but this program can only set two
  178. Xof them.  The third is the "last inode-modification" time and cannot be
  179. Xexplicitly overwritten by any program.
  180. X.SH AUTHOR
  181. XSteve B. Alter (Citicorp/TTI, Santa Monica)
  182. X.SH IDEAS
  183. XA variation on this program called
  184. X.B rcpmod
  185. Xwould be the TCP/IP network brother to
  186. X.B cpmod
  187. Xwhich works strictly through the UNIX file hierarchy (including SUN NFS.)
  188. X.B Rcpmod
  189. Xwould relate to
  190. X.B cpmod
  191. Xjust like
  192. X.B rcp
  193. Xrelates to
  194. X.BR cp .
  195. SHAR_EOF
  196. if test 1126 -ne "`wc -c < 'cpmod.1'`"
  197. then
  198.     echo shar: "error transmitting 'cpmod.1'" '(should have been 1126 characters)'
  199. fi
  200. fi
  201. echo shar: "extracting 'cpmod.c'" '(1789 characters)'
  202. if test -f 'cpmod.c'
  203. then
  204.     echo shar: "will not over-write existing file 'cpmod.c'"
  205. else
  206. sed 's/^X//' << \SHAR_EOF > 'cpmod.c'
  207. X/*
  208. X *  cpmod.c -- copy modes, ownerships and times from one file to others,
  209. X *           without affecting the data.
  210. X *
  211. X *  by Steve Alter (13-Aug-87, Citicorp/TTI, Santa Monica, CA)
  212. X *
  213. X */
  214. X
  215. X#include <stdio.h>
  216. X#include <sys/types.h>
  217. X#include <sys/stat.h>
  218. X#include <sys/time.h>
  219. X
  220. Xmain (argc, argv)
  221. X    int argc;
  222. X    char **argv;
  223. X{
  224. X    char *prog_name, *rindex();
  225. X    char *source_file;
  226. X
  227. X    short no_modes = 0;
  228. X    short no_owner = 0;
  229. X    short no_times = 0;
  230. X    short error = 0;
  231. X
  232. X    struct stat source_buf, new_buf;
  233. X    struct timeval tvp[2];
  234. X
  235. X    if (prog_name = rindex(argv[0], '/'))
  236. X        prog_name++;
  237. X    else
  238. X        prog_name = argv[0];
  239. X
  240. X    for (argc--, argv++; (argc && **argv == '-'); argc--, argv++)
  241. X        switch ((*argv)[1]) {
  242. X            case 'm':
  243. X                no_modes++;
  244. X                break;
  245. X            case 'o':
  246. X                no_owner++;
  247. X                break;
  248. X            case 't':
  249. X                no_times++;
  250. X                break;
  251. X            default:
  252. X                fprintf(stderr,
  253. X                    "%s: invalid option \"%s\"\n",
  254. X                    prog_name, *argv);
  255. X                error++;
  256. X        }
  257. X
  258. X    if (argc < 2 || error) {
  259. X        fprintf(stderr,
  260. X            "usage: %s [-m] [-o] [-t] file other-files ...\n",
  261. X            prog_name);
  262. X        exit(1);
  263. X    }
  264. X
  265. X    if (no_modes && no_owner && no_times) {
  266. X        fprintf(stderr,
  267. X        "%s: all three options disabled, no work left.\n",
  268. X            prog_name);
  269. X        exit(1);
  270. X    }
  271. X
  272. X    source_file = *argv++;
  273. X    argc--;
  274. X
  275. X    if (stat(source_file, &source_buf)) {
  276. X        perror(source_file);
  277. X        exit(1);
  278. X    }
  279. X
  280. X    while (argc--) {
  281. X        if (stat(*argv, &new_buf)) {
  282. X            perror(*argv++);
  283. X            continue;
  284. X        }
  285. X        if (!no_modes)
  286. X            if (chmod(*argv, (int)source_buf.st_mode))
  287. X                perror(*argv);
  288. X        if (!no_owner)
  289. X            if (chown(*argv, (int)source_buf.st_uid,
  290. X                     (int)source_buf.st_gid))
  291. X                perror(*argv);
  292. X        if (!no_times) {
  293. X            tvp[0].tv_sec = source_buf.st_atime;
  294. X            tvp[1].tv_sec = source_buf.st_mtime;
  295. X            tvp[0].tv_usec = tvp[1].tv_usec = 0L;
  296. X            if (utimes(*argv, tvp))
  297. X                perror(*argv);
  298. X        }
  299. X        argv++;
  300. X    }
  301. X}  /* main */
  302. SHAR_EOF
  303. if test 1789 -ne "`wc -c < 'cpmod.c'`"
  304. then
  305.     echo shar: "error transmitting 'cpmod.c'" '(should have been 1789 characters)'
  306. fi
  307. fi
  308. exit 0
  309. #    End of shell archive
  310.  
  311. -- 
  312.  
  313. Rich $alz
  314. Cronus Project, BBN Labs            rsalz@bbn.com
  315. Moderator, comp.sources.unix            sources@uunet.uu.net
  316.